# pip install jieba wordcloud -i https://mirrors.aliyun.com/pypi/simple/ 安装两个依赖包
from wordcloud import WordCloud
import numpy as np
from PIL import Image
import jieba
import csv
from collections import Counter

with open("d:/项目/python/爬虫/网页/豆瓣短评.csv", 'r', encoding='utf-8-sig') as csvfile:
    reader = csv.reader(csvfile)
    comment_list=list()
    for row in reader:
        comment_list.append(row[2])
comments=" ".join(comment_list) 
seg_list = list(jieba.cut(comments))

# 使用 Counter 统计词频并过滤掉长度小于2的词语
word_counter = Counter(word for word in seg_list if len(word) >= 2)
# 按出现次数降序排序并筛选出频率大于10的词语
filtered_word_dict = {word: freq for word, freq in word_counter.items() if freq > 10}
# 加载心形遮罩图像
mask_image = np.array(Image.open("d:/项目/python/制作词云/底图.png"))  # 替换为你的遮罩图像路径
# 创建词云对象并应用遮罩
wc = WordCloud(
    font_path="d:/项目/python/制作词云/simkai.ttf",  # 设置字体路径以支持中文
    background_color="white",
    width=1000,
    height=800,
    max_words=200,
    max_font_size=150,
    mask=mask_image,  # 应用遮罩
    contour_width=3,  # 可选：轮廓宽度
    contour_color='red'  # 可选：轮廓颜色
)
# 使用词频字典生成词云
word_cloud = wc.generate_from_frequencies(filtered_word_dict)
# 保存词云图片
word_cloud.to_file("d:/项目/python/制作词云/心.png")